package com.os.daoimpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.os.dao.FoodDaoS;
import com.os.model.FoodS;
import com.os.util.JdbcConUtilM;

public class FoodDaoImplS implements FoodDaoS {

	@Override
	public List<FoodS> selectAllFoodsS(int page,int rows) {
		List<FoodS> list = new ArrayList<FoodS>();
		StringBuffer sql = new StringBuffer();
		sql.append("select * from o_food");
		sql.append(" limit ?,?");
		Connection con = JdbcConUtilM.getCon();
		PreparedStatement pst = null;
		try {
			pst = con.prepareStatement(sql.toString());
			pst.setInt(1, (page-1)*rows);
			pst.setInt(2, rows);
			ResultSet re = pst.executeQuery();
			while(re.next()){
				FoodS f = new FoodS();
				f.setFid(re.getInt("fid"));
				f.setFname(re.getString("fname"));
				f.setFtype(re.getString("ftype"));
				f.setFspecialty(re.getInt("fspecialty"));
				f.setFurl(re.getString("furl"));
				f.setFnew(re.getInt("fnew"));
				f.setFprice(re.getDouble("fprice"));
				f.setFmaterial(re.getString("fmaterial"));
				f.setFhasma(re.getInt("fhasma"));
				list.add(f);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		JdbcConUtilM.closePstAndCon(pst, con);
		return list;
	}

	@Override
	public int selectFoodCountS() {
		int flag = 0;
		StringBuffer sql = new StringBuffer();
		sql.append("select  count(fid) scount from o_food");
		Connection con = JdbcConUtilM.getCon();
		PreparedStatement pst = null;
		try {
			pst = con.prepareStatement(sql.toString());
			ResultSet re = pst.executeQuery();
			if(re.next()){
				flag = re.getInt("scount");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		JdbcConUtilM.closePstAndCon(pst, con);
		return flag;
	}

	@Override
	public boolean addFoodS(String fname, String ftype, int fspecialty, String furl, int fnew, double fprice,
			String fmaterial, int fhasma) {
		boolean flag = false;
		StringBuffer sql = new StringBuffer();
		sql.append("INSERT INTO o_food (fname,ftype,fspecialty,furl,fnew,fprice,fmaterial,fhasma) ");
		sql.append(" VALUES (?,?,?,?,?,?,?,?)");
		Connection con = JdbcConUtilM.getCon();
		PreparedStatement pst = null;
		try {
			pst = con.prepareStatement(sql.toString());
			pst.setString(1, fname);
			pst.setString(2, ftype);
			pst.setInt(3, fspecialty);
			pst.setString(4, furl);
			pst.setInt(5, fnew);
			pst.setDouble(6, fprice);
			pst.setString(7, fmaterial);
			pst.setInt(8, fhasma);
			int i = pst.executeUpdate();
			if(i>0){
				flag = true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		JdbcConUtilM.closePstAndCon(pst, con);
		return flag;
	}

	@Override
	public boolean delFoodS(int fid) {
		boolean flag = false;
		StringBuffer sql = new StringBuffer();
		sql.append("DELETE FROM o_food WHERE o_food.fid = ?");
		Connection con = JdbcConUtilM.getCon();
		PreparedStatement pst = null;
		try {
			pst = con.prepareStatement(sql.toString());
			pst.setInt(1, fid);
			int i = pst.executeUpdate();
			if(i>0){
				flag = true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		JdbcConUtilM.closePstAndCon(pst, con);
		return flag;
	}

	@Override
	public boolean updateFoodS(String fname, String ftype, int fspecialty, String furl, int fnew, double fprice,
			String fmaterial, int fhasma, int fid) {
		boolean flag = false;
		StringBuffer sql = new StringBuffer();
		sql.append("UPDATE o_food SET fname=?,ftype=?,");
		sql.append(" fspecialty=?,furl=?,");
		sql.append(" fnew=?,fprice=?,");
		sql.append(" fmaterial=?,fhasma=?");
		sql.append(" WHERE fid = ?");
		Connection con = JdbcConUtilM.getCon();
		PreparedStatement pst = null;
		try {
			pst = con.prepareStatement(sql.toString());
			pst.setString(1, fname);
			pst.setString(2, ftype);
			pst.setInt(3, fspecialty);
			pst.setString(4, furl);
			pst.setInt(5, fnew);
			pst.setDouble(6, fprice);
			pst.setString(7, fmaterial);
			pst.setInt(8, fhasma);
			pst.setInt(9, fid);
			int i = pst.executeUpdate();
			if(i>0){
				flag = true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		JdbcConUtilM.closePstAndCon(pst, con);
		return flag;
	}

	@Override
	public int getFidByFnameH(String fname) {
		int fid = 0;
		StringBuffer sql = new StringBuffer();
		sql.append("select fid from o_food");
		sql.append(" where fname=?");
		Connection con = JdbcConUtilM.getCon();
		try {
			PreparedStatement ps = con.prepareStatement(sql.toString());
			ps.setString(1, fname);
			ResultSet rs = ps.executeQuery();
			if (rs.next()) {
				fid = rs.getInt("fid");
			}
			JdbcConUtilM.closePstAndCon(ps, con);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return fid;
	}

}
